package com.mo.admin.security.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mo.admin.api.user.domain.User;
import com.mo.admin.api.user.service.UserService;
import com.mo.admin.security.domain.UserDetailsImpl;
import jakarta.annotation.Resource;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

/**
 * 在这里查询数据库里面的用户信息
 */
@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 当前数据库中要求唯一的是phone，根据设计即可
        LambdaQueryWrapper<User> wrapper = Wrappers.<User>lambdaQuery().eq(User::getPhone, username);
        User user = userService.getOne(wrapper);
        if (user == null) {
            throw new UsernameNotFoundException("当前用户不存在");
        }
        return new UserDetailsImpl(user);
    }
}